using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XFGameFramework;

namespace XFGameFramework.ReddotSystem
{
    /// <summary>
    /// 红点对象(当需要显示时,会设置所有子节点为活跃状态,否则会把所有子节点设置为非活跃状态)
    /// </summary>
    [HelpURL("https://gitee.com/xianfengkeji/xfgameframework.reddot-system/blob/master/Documentation~/ClassApi/Reddot.md")]
    public class Reddot : MonoBehaviour
    { 
        #region 字段

        [SerializeField]
        [Tooltip("路径")] 
        private string _path;
 
        #endregion

        #region 属性

        /// <summary>
        /// 当前红点的路径
        /// </summary>
        public string Path 
        {
            get 
            { 
                return _path;
            }
            set 
            {
                _path = value; 
                Refresh();
            } 
        }

        #endregion

        #region 生命周期
  
        private void OnEnable()
        {
            Refresh(); 
            EventManager.AddEvent(ReddotManager.ON_REDDOT_PATH_VALUE_CHANGE, Refresh);
        }

        private void OnDisable()
        { 
            EventManager.RemoveEvent(ReddotManager.ON_REDDOT_PATH_VALUE_CHANGE, Refresh);
        }

        #endregion
    
        #region 方法

        private void Refresh() 
        {
            if (!_path.EndsWith("/"))
                _path = string.Format("{0}/", _path); // 添加一个结尾符

            bool show = ReddotManager.ShouldBeDisplay(Path);
            SetChildActive(show);
        }

        /// <summary>
        /// 移除红点
        /// </summary>
        public void Remove() 
        {
            ReddotManager.RemoveTipInternal(Path);
            Refresh();
        }

        private void SetChildActive(bool active) 
        {
            for (int i = 0; i < transform.childCount; i++)
            {
                Transform child = transform.GetChild(i);
                if(child == null) continue;
                child.gameObject.SetActive(active);
            }
        }


        #endregion
    
    }
}

